package com.itheima.ums.controller;

import cn.hutool.core.collection.CollUtil;
import com.itheima.mall.pojo.Resource;
import com.itheima.mall.pojo.ResourceCategory;
import com.itheima.ums.service.UmsResourceCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/resourceCategory")
public class UmsResourceCategoryController {

    @Autowired
    private UmsResourceCategoryService resourceCategoryService;

    /**
     * 添加后台资源分类
     *
     * @param resourceCategory
     * @return
     */
    @PostMapping("/create")
    public ResponseEntity create(@RequestBody ResourceCategory resourceCategory) {
        Integer count = resourceCategoryService.create(resourceCategory);
        return ResponseEntity.ok(count);
    }

    /**
     * 根据ID删除后台资源
     *
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public ResponseEntity deleteById(@PathVariable("id") Long id) {
        Integer count = resourceCategoryService.deleteById(id);
        return ResponseEntity.ok(count);

    }

    /**
     * 查询所有后台资源分类
     *
     * @return
     */
    @GetMapping("listAll")
    public ResponseEntity listAll() {
        List<ResourceCategory> list = resourceCategoryService.listAll();
        if (CollUtil.isEmpty(list)) {
            return ResponseEntity.ok("ok");
        }
        return ResponseEntity.ok(list);
    }

    /**
     * 修改后台资源分类
     *
     * @param id
     * @return
     */
    @PostMapping("/update/{id}")
    public ResponseEntity updateById(@PathVariable("id") Long id, @RequestBody ResourceCategory resourceCategory) {
        Integer count = resourceCategoryService.updateById(id, resourceCategory);
        if (count <= 0) {
            return ResponseEntity.ok("ok");
        }
        return ResponseEntity.ok(count);
    }
}
